<template>
  <el-dialog
    v-el-drag-dialog
    title="订单转派"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="100px">
      <el-form-item label="责任机构:" prop="orgId">
        <el-tree-select
          v-model="temp.orgId"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: false,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props:{
              value:'orgId',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @node-click="selectTreeChange"
        />
      </el-form-item>
      <el-form-item label="处理人:" prop="targetUserId">
        <el-select v-model="temp.targetUserId" :loading="$loadingPlugin.getOwnersList" style="width: 100%;" placeholder="请选择">
          <el-option
            v-for="item in ownersList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上次驳回原因:" prop="reason">
        <el-input v-model="currentRow.dismissedReason" disabled type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item label="驳回次数:" prop="reason">
        <el-input v-model="currentRow.dismissedCount" disabled type="text" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryRepairOrderTransfer, repairUserList } from '@/api'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'

  export default {
    name: 'transfer-order',
    data() {
      return {
        visible: false,
        treeData: [],
        ownersList: [],
        userId: '',
        repairCode: '',
        temp: {
          orgId: '',
          targetUserId: ''
        },
        rules: {
          orgId: [{ required: true, message: '请选择组织机构', trigger: 'change' }],
          targetUserId: [{ required: true, message: '请选择处理人', trigger: 'change' }]
        },
        currentRow: {}
      }
    },
    computed: {
      ...mapGetters([
        'orgList', 'corpSettingData'
      ])
    },
    methods: {
      async open(temp) {
        await Promise.all([this.getTreeData()])
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.currentRow = temp
            this.userId = temp.userId
            this.repairCode = temp.repairCode
            this.temp.orgId = temp.orgId
            this.getOwnersList(temp.orgId)
          }
        })
      },
      // 获取责任机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 选择所属机构
      selectTreeChange(value) {
        this.temp.orgId = value.orgId
        this.getOwnersList(value.orgId)
        this.$set(this.temp, 'targetUserId', '')
      },
      // 查询处理人列表
      async getOwnersList(orgId) {
        const res = await repairUserList({
          orgId,
          repairCode: this.currentRow.repairCode
        })
        this.ownersList = res.data.filter(item => item.userId !== this.userId)
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const org = this.orgList.find(item => item.orgId === this.temp.orgId)
          const params = {
            repairCode: this.repairCode,
            orgId: org.orgId,
            orgSNNo: org.orgSNNo,
            orgName: org.orgName,
            userId: this.userId,
            targetUserId: this.temp.targetUserId
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryRepairOrderTransfer(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.userId = ''
        this.repairCode = ''
        this.temp = {
          orgId: '',
          newOwnerId: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
